﻿class FlyStar {
	
	//---------------- 属性定义
	[Inspectable(defaultValue="_root")]
	public var createTarget:MovieClip;
	//产生角色目标
	[Inspectable(defaultValue="snow")]
	public var subMcName:Array;
	//角色名数组
	[Inspectable(defaultValue="false")]
	public var randFlyingMc:Boolean;
	//随机角色
	[Inspectable(defaultValue="v:1,rv:1")]
	public var flyRotation:Object ;
	//角色随机角度旋转
	[Inspectable(defaultValue="v:1,rv:1")]
	public var flyVx:Object;
	//角色速度
	[Inspectable(defaultValue="v:5,rv:5")]
	public var flyVy:Object;
	//角色速度
	[Inspectable(defaultValue="500")]
	public var createMcV:Number;
	//角色产生速度
	[Inspectable(defaultValue="lx:1,ly:1,lscal:100,rx:200,ry:1,rscal:100,randomS:1")]
	public var startPoints:Object;
	//起始点位置(两点一线)
	[Inspectable(defaultValue="v:50,rv:100")]
	public var flyScale:Object;
	//角色运动中个体变化
	[Inspectable(defaultValue="0.1")]
	public var flyVC:Number;
	//角色速度根随个体变化率
	[Inspectable(defaultValue="200")]
	public var life:Number;
	//主角生命(运动长度)
	[Inspectable(defaultValue="10")]
	public var myDep:Number;
	//自身深度
	//----------------- 变量部分
	private var me:Object = new Object();
	//根路径
	private var runID:Number;
	//工作IntervalId
	private var _pause:Boolean = false;
	//主角暂停
	private var _enable:Boolean = true;
	//是否允许继续制造主角
	private var _ifwork:Boolean = false;
	//工作开使开关
	//----------------- 运动参数部分
	private var cjsq:Number = 0;
	//层计数器
	private var tag:MovieClip;
	//产生主角的mc
	private var bulidID:Number;
	//生产IntervalId
	private var nowNum:Number = 0;
	//当前已经有的个数
	private var startW:Number = 0;
	//x值差
	private var startH:Number = 0;
	//y值差
	private var startS:Number = 0;
	//scal值差
	//------------------  初始化参数函数
	public function initVar() {
		startW = startPoints.rx-startPoints.lx;
		startH = startPoints.ry-startPoints.ly;
		startS = startPoints.lscal-startPoints.rscal;
	};
	//------------------ 构造函数
	public function FlyStar() {
		trace("类初始化");
		me = this;
		
	};
	//------------------- 运动函数
	private function startMe() {
		runID = setInterval(runingMe, 10, me);
	};
	private function runingMe(me:Object) {
	};
	private function stopMe() {
		clearInterval(runID);
	};
	//-------------------- 生产主角函数
	private function bulidMc() {
		bulidID = setInterval(buliding, createMcV, me);
	};
	private function buliding(me:Object) {
		if (!me._pause && _enable) {
			if (me.randFlyingMc) {
				//随机主角
			} else {
				//非随机主角
				var tag1 = me.tag.attachMovie(me.subMcName[0], "____snow"+me.cjsq.toString(), me.cjsq);
				tag1.xv = me.flyVx.v+Math.random()*me.flyVx.rv;
				tag1.yv = me.flyVy.v+Math.random()*me.flyVy.rv;
				tag1.r = me.flyRotation.v+Math.random()*me.flyRotation.rv;
				if (me.startPoints.lx == me.startPoints.rx && me.startPoints.ly == me.startPoints.ry) {
					//起点为一点
					tag1._x = me.startPoints.lx;
					tag1._y = me.startPoints.ly;
				} else {
					if (me.startPoints.lx != me.startPoints.rx) {
						tag1._x = (me.startW)*Math.random()+me.startPoints.lx;
						tag1._y = (me.startH)*Math.random()+me.startPoints.ly;
					}
				}
				//-------------- 记录起点
				tag1.sx = tag1._x;
				tag1.sy = tag1._y;
				var s = ((me.startS*(tag1._x-me.startPoints.lx)/(me.startW))+(me.startS*(tag1._y-me.startPoints.ly)/(me.startH)))/2;
				tag1._xscale = tag1._yscale=s+me.startPoints.randomS*Math.random();
				tag1.sv = me.flyScale.v+Math.random()*me.flyScale.rv;
				cjsq++;
			}
		} else {
			trace("暂停或不允许创建新的主角");
		}
	};
	private function endBulid() {
		clearInterval(bulidID);
	};
	//-------------------- get set
	public function set ifwork(b:Boolean) {
		trace("开始");
		if (b) {
			createTarget.createEmptyMovieClip("___snowCOM", myDep);
			tag = createTarget.___snowCOM;
			bulidMc();
			startMe();
		} else {
			createTarget.___snowCOM.removeMovieClip();
			tag = null;
			stopMe();
			endBulid();
		}
	};
	public function get ifwork() {
		return _ifwork;
	};
}
